package com.special.ResideMenuDemo;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.map.MapController;
import com.baidu.mapapi.map.MapView;
import com.baidu.platform.comapi.basestruct.GeoPoint;
import com.special.base.Application;
import com.special.view.composerLayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;

/**
 * User: special Date: 13-12-22 Time: 下午3:26 Mail: specialcyci@gmail.com
 */
public class MapFragment extends Fragment {

	private BMapManager mBMapMan;
	private MapView mMapView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (mBMapMan == null) {
			mBMapMan = new BMapManager(getActivity().getApplicationContext());
			mBMapMan.init(Application.MAPKEY, null);
		}
		// 注意：请在试用setContentView前初始化BMapManager对象，否则会报错
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.map, container, false);
		
		//初始化地图控件
		mMapView = (MapView) view.findViewById(R.id.bmapsView);
		mMapView.setBuiltInZoomControls(true);
		// 设置启用内置的缩放控件
		MapController mMapController = mMapView.getController();
		// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
		GeoPoint point = new GeoPoint((int) (39.915 * 1E6),
				(int) (116.404 * 1E6));
		// 用给定的经纬度构造一个GeoPoint，单位是微度 (度 * 1E6)
		mMapController.setCenter(point);// 设置地图中心点
		mMapController.setZoom(12);// 设置地图zoom级别
		
		
		//初始化pathButton
		composerLayout clayout=(composerLayout)view.findViewById(R.id.test);
        clayout.init(
        		new int[]{
        				R.drawable.composer_camera,
        				R.drawable.composer_music,
        				R.drawable.composer_place,
        				R.drawable.composer_sleep,
        				R.drawable.composer_thought,
        				R.drawable.composer_with
        				}, 
        		R.drawable.composer_button, 
        		R.drawable.composer_icn_plus, 
        		composerLayout.RIGHTCENTER ,
        		100, 
        		100);

        OnClickListener clickit=new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(v.getId()==100+0){
					System.out.println("composer_camera");
				}else if(v.getId()==100+1){
					System.out.println("composer_music");
				}else if(v.getId()==100+2){
					System.out.println("composer_place");
				}else if(v.getId()==100+3){
					System.out.println("composer_sleep");
				}else if(v.getId()==100+4){
					System.out.println("composer_thought");
				}else if(v.getId()==100+5){
					System.out.println("composer_with");
				}
			}
		};
		clayout.setButtonsOnClickListener(clickit);
		
		//测试父控件是否能获取点击
		RelativeLayout rl=(RelativeLayout)view.findViewById(R.id.rlparent);
		rl.setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				System.out.println("父控件可以获取点击事件");
			}
		});
		return view;
	}
	@Override
	public void onDestroy(){
	        mMapView.destroy();
	        if(mBMapMan!=null){
	                mBMapMan.destroy();
	                mBMapMan=null;
	        }
	        super.onDestroy();
	}
	@Override
	public void onPause(){
	        mMapView.onPause();
	        if(mBMapMan!=null){
	               mBMapMan.stop();
	        }
	        super.onPause();
	}
	@Override
	public void onResume(){
	        mMapView.onResume();
	        if(mBMapMan!=null){
	                mBMapMan.start();
	        }
	       super.onResume();
	}

}
